package gel;

import static org.junit.Assert.assertTrue;
import java.awt.geom.Point2D;
import robocode.control.events.RoundEndedEvent;
import robocode.control.events.TurnEndedEvent;
import robocode.control.snapshot.IRobotSnapshot;
import gel.test.RobotTestBed;

/**
 * Tests that the robot moves if fired upon by the sample.Fire robot.
 * @author George Lee
 *
 */
public class TestMenehuneDodge extends RobotTestBed {
  /** Threshold of dodges in a round. */
  public static final int DODGE_THRESHOLD = 2;
  /** Count how many times we reposition when fired at. */
  int dodgeCount = 0;
  /** Previous coordinates for our robot. */
  Point2D.Double initialStrikingLocation;

  /**
   * @see gel.test.RobotTestBed#getRobotNames()
   * @return The comma delimited list of robots in the match.
   */
  @Override
  public String getRobotNames() {
    return "sample.Fire,gel.Menehune";
  }

  /**
   * This test runs for 5 rounds.
   * 
   * @return The number of rounds.
   */
  @Override
  public int getNumRounds() {
    return 5;
  }

  /**
   * Check that robot moves to striking distance first. Then check that the robot moved from
   * striking distance.
   * 
   * @param event Info about the current state of the battle.
   */
  @Override
  public void onTurnEnded(TurnEndedEvent event) {
    IRobotSnapshot fire = event.getTurnSnapshot().getRobots()[0];
    IRobotSnapshot menehune = event.getTurnSnapshot().getRobots()[1];

    Point2D.Double menehuneLocation = new Point2D.Double(menehune.getX(), menehune.getY());
    Point2D.Double fireLocation = new Point2D.Double(fire.getX(), fire.getY());

    if (initialStrikingLocation == null) {
      double distance = menehuneLocation.distance(fireLocation);
      if (distance <= Menehune.MIN_DISTANCE) {
        initialStrikingLocation = menehuneLocation;
      }
    }
    // We were in striking distance. Check to see that we moved from it.
    else if (!menehuneLocation.equals(initialStrikingLocation)) {
      dodgeCount++;
    }
  }

  /**
   * After the round, check to see that we dodged bullets.
   * 
   * @param event Details about the completed battle.
   */
  @Override
  public void onRoundEnded(RoundEndedEvent event) {
    assertTrue("Checking dodge count.", dodgeCount > DODGE_THRESHOLD);
    dodgeCount = 0;
  }
}
